/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import java.util.Date;

import org.starsource.forum.client.domain.UserObject;
import org.starsource.forum.client.exception.ExceptionDescription;
import org.starsource.forum.client.handler.SignInHandler;
import org.starsource.forum.client.web.UserService;
import org.starsource.forum.client.web.UserServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;

/**
 * A simple example of an 'about' dialog box.
 */
public class SignInDialog extends DialogBox {
	private final UserServiceAsync userService = GWT.create(UserService.class);

	interface Binder extends UiBinder<Widget, SignInDialog> { }
	private static final Binder binder = GWT.create(Binder.class);
	interface RichTextBoxStyle extends CssResource {
			String richTextArea();
	}

	@UiField TextBox accountTextBox;
	@UiField PasswordTextBox passwordTextBox;
	@UiField CheckBox autoSignIn;
	@UiField Button loginButton;
	@UiField Label tips;
	
	private SignInHandler signInHandler;
	
	public void setSignInHandler(SignInHandler signInHandler) {
		this.signInHandler = signInHandler;
	}

	public SignInDialog() {
		
		setWidget(binder.createAndBindUi(this));
		
		// Use this opportunity to set the dialog's caption.
		setText("请您登录");
		
		accountTextBox.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					passwordTextBox.setFocus(true);
				}
			}
			
		});
		
		passwordTextBox.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					login();
				}
			}
			
		});
		
		
		setAnimationEnabled(true);
		setGlassEnabled(true);
	}

	@UiHandler("loginButton")
	void onLoginClicked(ClickEvent event) {
		login();
	}

	public void focus() {
		accountTextBox.setFocus(true);
	}
	private void login() {
		loginButton.setEnabled(false);
		tips.setText("");
		userService.login(accountTextBox.getText(), passwordTextBox.getText(), new AsyncCallback<UserObject>() {

			@Override
			public void onFailure(Throwable caught) {
				tips.setText(ExceptionDescription.getDescription(caught));
				loginButton.setEnabled(true);
			}
	
			@Override
			public void onSuccess(UserObject result) {
				loginButton.setEnabled(true);
				hide();
					if ("1234".equals(passwordTextBox.getText())) {
						PasswordUpdateDialog change = new PasswordUpdateDialog();
						change.center();
					}
	
				if (autoSignIn.getValue().booleanValue()) {
					setCookie(result);
				}
				signInHandler.onSignIn(result);
				
			}
			
		});
	}
	
	private void setCookie(UserObject user) {
		Date expires = new Date();
		CalendarUtil.addDaysToDate(expires, 14);
		Cookies.setCookie("account", user.getAccount(), expires);
		Cookies.setCookie("authkey", user.getPassword(), expires);
	}
	
	public void clearUserInput() {
		accountTextBox.setText("");
		passwordTextBox.setText("");
	}
	
	public void authenticate() {
		String account = Cookies.getCookie("account");
		String authKey = Cookies.getCookie("authkey");
		
		if (account == null || authKey == null) {
			autoSignIn.setValue(false);
			return;
		}

		loginButton.setEnabled(false);
		accountTextBox.setText(account);
		passwordTextBox.setText(authKey);

		userService.authenticate(account, authKey, new AsyncCallback<UserObject>() {

			@Override
			public void onFailure(Throwable caught) {
				loginButton.setEnabled(true);
			}

			@Override
			public void onSuccess(UserObject result) {
				setCookie(result);					
				hide();
				loginButton.setEnabled(true);
				signInHandler.onSignIn(result);
			}
			
		});
	}
	
}
